package deviceScript

import (
	"gitee.com/fierce_wolf/go-fox-edge-common/commUtil/Map"
	"github.com/dop251/goja"
	"sync"
)

type ScriptEngineService struct {
	mutex     sync.Mutex
	engineMap map[string]interface{}
}

func (e *ScriptEngineService) getScriptEngine(manufacturer string, deviceType string, routineKey string) *goja.Runtime {
	e.mutex.Lock()
	defer e.mutex.Unlock()

	vm, _ := Map.GetValue(e.engineMap, manufacturer, deviceType, routineKey)
	if vm != nil {
		return vm.(*goja.Runtime)
	}

	engine := goja.New()
	Map.SetValue(e.engineMap, manufacturer, deviceType, routineKey, engine)
	return engine
}
